package com.gx.common.exception;


import com.gx.common.enums.ResultCodeEnum;
import com.gx.common.result.AppResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author Liu-pj
 * @Date 16:02
 **/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionControllerAdvice {

    @ExceptionHandler(value = CommonException.class)
    public AppResult<String> handleException(CommonException e) {
        return AppResult.failure(e.getMsg(), e.getCode());
    }

    @ExceptionHandler(value = FlowException.class)
    public AppResult<String> handleException(FlowException e) {
        return AppResult.failure(e.getMsg(), e.getCode());
    }

    @ExceptionHandler(value = Exception.class)
    public AppResult<String> exception(Exception e) {
        e.printStackTrace();
        log.error("异常信息：{}，异常类型：{}", e.getMessage(), e.getClass());
        //返回通用异常
        return AppResult.failure(ResultCodeEnum.UNKNOWN_REASON);
    }

    @ExceptionHandler(value = Throwable.class)
    public AppResult<String> handleException(Throwable e) {
        e.printStackTrace();
        log.error("异常信息：{}，异常类型：{}", e.getMessage(), e.getClass());
        return AppResult.failure(ResultCodeEnum.INTERNAL_SERVER_ERROR);
    }

}
